var btnUploadText = '选择封面';
$(document).ready(function() {
        $("#uploadButton").text(btnUploadText)
        $(".imageShow").hide()
        $('#uploadButton').click(function() {
            $('#imagePic').click();
        });
        $("#imagePic").on("change", function(e) {
            var file = e.target.files[0]; //获取图片资源
            console.log(file)
            var fileTypes = ["bmp", "jpg", "png", "jpeg"];
            var bTypeMatch = false
            for (var i = 0; i < fileTypes.length; i++) {
                var start = file.name.lastIndexOf(".");
                var fileType = file.name.substring(start + 1);
                if (fileType.toLowerCase() == fileTypes[i]) {
                    bTypeMatch = true;
                    break;
                }
            }
            if (bTypeMatch) {
                // 不得大于10MB
                if (file.size <= 1024 * 1024 * 10) {
                    var reader = new FileReader();
                    reader.readAsDataURL(file); // 读取文件
                    // 渲染文件
                    reader.onload = function(arg) {
                        $(".imageShow").show()
                        $("#uploadImageShow").attr("src", arg.target.result)
                        $("#uploadImageShow").attr("height", "200px")
                        btnUploadText = '重新选择'
                        $("#uploadButton").text(btnUploadText)
                    }
                } else {
                    alert('仅支持不超过10M的图片');
                    emptyImageUpload("#imagePic")
                    $("#uploadImageShow").attr("src", "")
                    $(".imageShow").hide()
                    btnUploadText = '上传'
                    $("#uploadButton").text(btnUploadText)
                    return false;
                }
            } else {
                alert('仅限bmp，jpg，png，jpeg图片格式');
                emptyImageUpload("#imagePic")
                $("#uploadImageShow").attr("src", "")
                $(".imageShow").hide()
                btnUploadText = '上传'
                $("#uploadButton").text(btnUploadText)
                return false;
            }
        });
    })
    //清空上传图片信息
function emptyImageUpload(selector) {
    var fi;
    var sourceParent;
    if (selector) {
        fi = $(selector);
        sourceParent = fi.parent();
    } else {
        return;
    }
    $("<form id='tempImgForm'></form>").appendTo(document.body);

    var tempImgForm = $("#tempImgForm");
    tempImgForm.append(fi);
    tempImgForm.get(0).reset();
    sourceParent.append(fi);
    tempImgForm.remove();
}